package com.ibm.ecm.configmgr.app.acm.widgets.rest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HttpContext;

import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;

import com.ibm.ecm.configmgr.app.acm.widgets.util.XMLUtils;
//import com.ibm.jvm.util.ByteArrayOutputStream;

public class Test {

	/**
	 * @param args
	 * @throws IOException 
	 */
//	public static void main(String[] args) throws Throwable {
////GET		http://9.181.141.74:9081/WorkplaceXT/P8BPMREST/p8/bpm/v1/currentuser
////		http://9.123.106.125:9080/WorkplaceXT/P8BPMREST/p8/bpm/v1/currentuser
//		String hostname = "9.123.113.113";
//		String port = "9080";
//		String uid = "administrator";
//		String pwd = "passw0rd!";
////		System.out.println(Test.checkCurrentUser(hostname,port, uid, pwd));
////		System.out.println(Test.checkAppSpace(hostname,port, uid, pwd,"deyanspace"));
////		System.out.println(Test.checkCommandService(hostname,port, uid, pwd));
//
//		System.out.println(Test.getWorkItemInfo(hostname, port, uid, pwd, "deyanqueue", "B924EB1F64EF944A930B512BF58707A2"));
//	}
	
//	public static boolean checkCurrentUser(String hostname, String port, String uid, String pwd) throws Throwable{
//		String url = "http://" + hostname + ":" + port + "/WorkplaceXT/P8BPMREST/p8/bpm/v1/currentuser";
//		System.out.println(url);
//		RESTUtilOnATOM util = new RESTUtilOnATOM();
//        Properties prop = new Properties();
//        prop.setProperty("HOSTNAME", hostname);
//        prop.setProperty("PORT", port);
//        prop.setProperty("USERNAME", uid);
//        prop.setProperty("PASSWORD", pwd);
//        util.setLoginUrl("http://"+hostname+":"+port+"/WorkplaceXT");
//		util.init(prop);
//		
//		HttpResponse resp = util.executeGet(url);
////		System.out.println(resp.getStatusLine());
//		HttpEntity entity = resp.getEntity();
//		InputStream in = entity.getContent();
//		String rtn = inputStream2String(in);
////		System.out.println(rtn);
//		String[] list = rtn.split(",");
//		if(list.length==2){
//			String[] list2 = list[1].split(":");
//			String uname = list2[1].substring(1, list2[1].length()-2);
////			System.out.println(uname.equalsIgnoreCase(uid));
//			return uname.equalsIgnoreCase(uid);
//		}
//		return false;
//	}

	/**
	 * Get work item info
	 * @param hostname
	 * @param port
	 * @param uid
	 * @param pwd
	 * @param workingQueue
	 * @param wobNum
	 * @return
	 * @throws Throwable
	 */
//	public static String getWorkItemInfo(String hostname, String port, String uid, String pwd,String workingQueue,String wobNum) throws Throwable{
////		http://9.123.113.113:9080/WorkplaceXT/P8BPMREST/p8/bpm/v1/
////		String url = "http://" + hostname + ":" + port + "/WorkplaceXT/P8BPMREST/p8/bpm/v1/currentuser";
//		String url = "http://" + hostname + ":" + port + "/WorkplaceXT/P8BPMREST/p8/bpm/v1/queues/"+ workingQueue +"/stepelements/" + wobNum;
//		System.out.println(url);
//		RESTUtilOnATOM util = new RESTUtilOnATOM();
//        Properties prop = new Properties();
//        prop.setProperty("HOSTNAME", hostname);
//        prop.setProperty("PORT", port);
//        prop.setProperty("USERNAME", uid);
//        prop.setProperty("PASSWORD", pwd);
//        util.setLoginUrl("http://"+hostname+":"+port+"/WorkplaceXT");
//		util.init(prop);
//		
//		HttpResponse resp = util.executeGet(url);
////		System.out.println(resp.getStatusLine());
//		HttpEntity entity = resp.getEntity();
//		InputStream in = entity.getContent();
//		String rtn = inputStream2String(in);
//		System.out.println(rtn);
////		String[] list = rtn.split(",");
////		if(list.length==2){
////			String[] list2 = list[1].split(":");
////			String uname = list2[1].substring(1, list2[1].length()-2);
//////			System.out.println(uname.equalsIgnoreCase(uid));
////			return uname.equalsIgnoreCase(uid);
////		}
//		return rtn;
//	}

//	public static boolean checkCommandService(String hostname, String port, String uid, String pwd) throws Throwable{
//		String url = "http://" + hostname + ":" + port + "/WorkplaceXT/integrationWebBasedCommand?_commandId=1100&_responseURL=" + "http://" + hostname + ":" + port + "/WorkplaceXT";
////		System.out.println(url);
//		RESTUtilOnATOM util = new RESTUtilOnATOM();
//        Properties prop = new Properties();
//        prop.setProperty("HOSTNAME", hostname);
//        prop.setProperty("PORT", port);
//        prop.setProperty("USERNAME", uid);
//        prop.setProperty("PASSWORD", pwd);
//        util.setLoginUrl("http://"+hostname+":"+port+"/WorkplaceXT");
//		util.init(prop);
//		
//		HttpResponse resp = util.executeGet(url);
//		String statusLine = resp.getStatusLine().toString();
//		System.out.println(statusLine);
//
//		HttpEntity entity = resp.getEntity();
//		InputStream in = entity.getContent();
//		String rtn = inputStream2String(in);
////		System.out.println(rtn);
//		if(rtn.indexOf("content=\"orion410")>0){
//			return true;
//		}else{
//			return false;
//		}
//	}

//	public static boolean checkAppSpace(String hostname, String port, String uid, String pwd,String spaceName) throws Throwable{
//		String url = "http://" + hostname + ":" + port + "/WorkplaceXT/P8BPMREST/p8/bpm/v1/appspaces";
//		System.out.println(url);
////		String url2 = "https://" + hostname + ":" + port + "/WorkplaceXT/P8BPMREST/p8/bpm/v1/currentuser";
//		RESTUtilOnATOM util = new RESTUtilOnATOM();
//        Properties prop = new Properties();
//        prop.setProperty("HOSTNAME", hostname);
//        prop.setProperty("PORT", port);
//        prop.setProperty("USERNAME", uid);
//        prop.setProperty("PASSWORD", pwd);
//
//        util.setLoginUrl("http://"+hostname+":"+port+"/WorkplaceXT");
//		util.init(prop);
//		
//		HttpResponse resp = util.executeGet(url);
//		System.out.println(resp.getStatusLine());
//		HttpEntity entity = resp.getEntity();
//		InputStream in = entity.getContent();
//		String rtn = inputStream2String(in);
////		System.out.println(rtn);
//		String newName = "\"" + spaceName + "\"";
//		if(rtn.indexOf(newName)>0){
//			return true;
//		}else{
//			return false;
//		}
//	}
//	

//	public static String inputStream2String(InputStream is) throws IOException {   
//		ByteArrayOutputStream baos = new ByteArrayOutputStream();   
//	    int i = -1;   
//	    try{
//		    while ((i = is.read()) != -1) {   
//		        baos.write(i);   
//		    }   	    	
//	    }catch(Exception e){
//	    	//do nothing
//	    }
////        System.out.println(baos.toString());
//	    return baos.toString();   
//	}  
	


}